<?php

require_once APPLICATION_PATH . '/../library/fb/facebook.php';

class Application_Model_UnionUser_facebook extends Application_Model_UnionUser_Base {

    protected $_appid='334737743338587';
    
    protected $_secret ='55891db80f70c72678db1432ef97f4ba';

    protected $_userId = '';

    public function getUnionName() {
        return 'Facebook';
    }

    public function getLoginUrl() {
        return 'http://uuch.local/user/unioncallback?name=facebook&reurl=';
    }
    
    public function getfbUrl($reurl){
        $facebook = new Facebook(array(
            'appId' => $this->_appid,
            'secret' => $this->_secret
        ));
        return $facebook->getLoginUrl(array('redirect_uri' => $this->getLoginUrl().urlencode($reurl)));
    }

    public function getUserInfo() {
        $facebook = new Facebook(array(
            'appId' => $this->_appid,
            'secret' => $this->_secret
        ));
        $user_profile = $facebook->api('/me');
        $user = new Application_Model_UnionUser_UserInfo();
        $user->nickname = $user_profile['username'];
        $user->userKey = $user_profile['id'];
        if(!isset($user->nickname) || $user->nickname == '' ||  $user->nickname == NULL){
            $user->nickname = $user_profile['name'];
        }
        return $user;
    }

    public function getUserKey() {
        return $this->_userId;
    }

    protected function get_url_contents($url) {
        ;
    }

    public function isValid() {
        $facebook = new Facebook(array(
            'appId' => $this->_appid,
            'secret' => $this->_secret
        ));
        // Get User ID
        $user = $facebook->getUser();
        $this->_userId = $user;
        
        if($user)
            return TRUE;
        else
            return FALSE;
    }

}